package ioc.demo.ioc;

import ioc.demo.bean.HelloWorld;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

/**
 * 描述:
 *
 * @author Zhanggq
 * @date 2019/9/19 11:05
 */
public class IoCHelloWorld {

    public void sayHello() {
        HelloWorld helloWorld = null;
        //加载Spring配置文件applicationContext.xml
        Resource resource = new ClassPathResource("applicationContext.xml");
        //创建Spring容器对象
        BeanFactory beanFactory = new XmlBeanFactory(resource);
        //从Spring容器中获取名为helloworld的bean
//        helloWorld = (HelloWorld) beanFactory.getBean("helloWorld");
//        helloWorld = (HelloWorld) beanFactory.getBean(ioc.demo.bean.HelloWorld.class);
        helloWorld = (HelloWorld) beanFactory.getBean("helloWorld", HelloWorld.class);
        helloWorld.sayHello();
    }
}
